//
//  BEAgreementCtl.m
//  s.
//
//  Created by fanshijian on 2022/9/20.
//  Copyright © 2022 J. All rights reserved.
//

#import "BEAgreementCtl.h"

@interface BEAgreementCtl () <WKUIDelegate,WKNavigationDelegate>
@property (nonatomic, strong) WKWebView *webView;
@property (strong, nonatomic) NSString *htmlData;
@property (strong, nonatomic) NSString *externalTitle; // 外部传过来的值

@property (weak, nonatomic) UIView *bottomView;
@property (weak, nonatomic) UIButton *btn1, *btn2;
@end

@implementation BEAgreementCtl

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = KLColor(@"#FFFFFF");
    
    self.htmlData = self.metaInfo[@"htmlData"];
    self.externalTitle = self.metaInfo[@"title"];
    
    self.title = self.externalTitle;
    
    [self setupView];
}

- (void)setupView {
    NSArray *views = @[self.btn1, self.btn2];
    [views mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:16 leadSpacing:12 tailSpacing:12];
    [views mas_makeConstraints:^(MASConstraintMaker *make) {
        kMakeHV(47);
        kMakeTV(12);
    }];
    
    [self.webView loadHTMLString:self.htmlData baseURL:nil];
}

- (WKWebView *)webView {
    if (!_webView) {
        WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
        configuration.allowsInlineMediaPlayback = NO;
        
        WKWebView *obj = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];
        [self.view addSubview:_webView = obj];
        obj.navigationDelegate = self;
        [obj mas_makeConstraints:^(MASConstraintMaker *make) {
            kMakeLTRV(0);
            kMakeBV(-(klSafeAreaEdgeInsets.bottom+71));
        }];
    }
    return _webView;
}

- (UIView *)bottomView {
    if (!_bottomView) {
        UIView *obj = [UIView new];
        [self.view addSubview:_bottomView = obj];
        [obj mas_makeConstraints:^(MASConstraintMaker *make) {
            kMakeLBRV(0);
            kMakeHV(klSafeAreaEdgeInsets.bottom+71);
        }];
    }
    return _bottomView;
}

- (UIButton *)btn1 {
    if (!_btn1) {
        UIButton *obj = [UIButton createBtnTitleColor:KLColor(@"#666666") font:U_bfont(16) title:@"不同意"];
        [self.bottomView addSubview:_btn1 = obj];
        [obj fsj_setRoundRadius:47.0*0.5 borderColor:KLColor(@"#666666") board:0.5];
        obj.zh_action = kMsgForward(@"clickCancel", WKProxy(self));
    }
    return _btn1;
}

- (UIButton *)btn2 {
    if (!_btn2) {
        UIButton *obj = [UIButton createBtnTitleColor:KLColor(@"#FFFFFF") font:U_bfont(16) title:@"我同意"];
        [self.bottomView addSubview:_btn2 = obj];
        [obj fsj_setRoundRadius:23 borderColor:[UIColor clearColor]];
        obj.backgroundColor = JYColor_Main();
        obj.zh_action = kMsgForward(@"clickConfirm", WKProxy(self));
    }
    return _btn2;
}

#pragma mark - EVENT
- (void)clickCancel {
    [self back];
}

- (void)clickConfirm {
    [self dismissViewControllerAnimated:YES completion:^{
        JYBoolBlock block = self.handle;
        if (block) {
            block(YES);
        }
    }];
}

@end
